package com.freight.order.serviceimpl;

import com.freight.core.common.constant.CommonConstant;
import com.freight.core.common.exception.FreightException;
import com.freight.core.common.utils.ObjectUtil;
import com.freight.core.common.utils.SecurityUtil;
import com.freight.core.entity.User;
import com.freight.msg.dao.MsgConsultingComplaintsDao;
import com.freight.msg.dao.MsgConsultingComplaintsRecordDao;
import com.freight.msg.dto.MsgConsultingComplaintsDto;
import com.freight.msg.dto.MsgConsultingDto;
import com.freight.msg.dto.MsgConsultingRecordDto;
import com.freight.msg.entity.MsgConsultingComplaints;
import com.freight.msg.entity.MsgConsultingComplaintsRecord;
import com.freight.msg.mapper.MsgConsultingMapper;
import com.freight.msg.service.MsgConsultingComplaintsService;
import com.freight.order.entity.OrderOrder;
import com.freight.order.service.OrderOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class MsgConsultingComplaintsServiceImpl implements MsgConsultingComplaintsService {

    @Autowired
    private MsgConsultingComplaintsDao msgConsultingComplaintsDao;



    @Autowired
    private OrderOrderService orderOrderService;

    @Autowired
    private MsgConsultingMapper msgConsultingMapper;

    @Autowired
    private MsgConsultingComplaintsRecordDao msgConsultingComplaintsRecordDao;



    @Autowired
    SecurityUtil securityUtil;

    @Override
    public MsgConsultingComplaintsDao getRepository() {
        return msgConsultingComplaintsDao;
    }


    @Override
    public void evaluate(Long msgConsultingId, Integer level) {
        MsgConsultingComplaints mcc=msgConsultingComplaintsDao.getOne(msgConsultingId);
        if (mcc!=null || mcc.getHandleState() == 3){
            mcc.setLevel(level);
            msgConsultingComplaintsDao.save(mcc);
        }
    }

    @Override
    public List<MsgConsultingComplaintsRecord> getmsgConsultingIdList(Long msgConsultingId) {

        List<MsgConsultingComplaintsRecord> res =msgConsultingMapper.getmsgConsultingIdList(msgConsultingId);

        return res;
    }

    @Override
    public Page<MsgConsultingComplaints> getHomeMsgList(Pageable pageable) {

        User u = securityUtil.getCurrUser();
        Page<MsgConsultingComplaints> res = msgConsultingComplaintsDao.findAll(new Specification<MsgConsultingComplaints>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<MsgConsultingComplaints> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {


                cq.orderBy(cb.desc(root.get("createTime")));
                return null;
            }
        }, pageable);

        return res;
    }

    @Override
    public Page<MsgConsultingComplaints> getMsgConsultingList(MsgConsultingComplaintsDto msgConsultingComplaintsDto, Integer handleState, Pageable pageable, String types) {

        User u = securityUtil.getCurrUser();
        Page<MsgConsultingComplaints> res = msgConsultingComplaintsDao.findAll(new Specification<MsgConsultingComplaints>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<MsgConsultingComplaints> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                Path<Long> userId = root.get("userId");
                Path<Long> msgType = root.get("msgType");
                Path<Integer> state = root.get("handleState");

                Path<Integer> type = root.get("type");
                Path<String> name = root.get("name");
                Path<String> phone = root.get("phone");
                Path<String> consultingNo = root.get("consultingNo");
                Path<String> problemType = root.get("problemType");

                List<Predicate> list = new ArrayList<>();
                if (u.getUserType() != CommonConstant.USER_TYPE_ADMIN){
                    list.add(cb.equal(userId, u.getId()));
                }
                if (handleState!= null){
                    list.add(cb.equal(state, handleState));
                }
                if(msgConsultingComplaintsDto!=null){
                    if(msgConsultingComplaintsDto.getType()!=null){
                        list.add(cb.equal(type, msgConsultingComplaintsDto.getType()));
                    }
                    if(msgConsultingComplaintsDto.getConsultingNo()!=null && !msgConsultingComplaintsDto.getConsultingNo().equals("")){
                        list.add(cb.equal(consultingNo, msgConsultingComplaintsDto.getConsultingNo()));
                    }
                    if(msgConsultingComplaintsDto.getName()!=null && !msgConsultingComplaintsDto.getName().equals("")){
                        list.add(cb.equal(name, msgConsultingComplaintsDto.getName()));
                    }
                    if(msgConsultingComplaintsDto.getPhone()!=null && !msgConsultingComplaintsDto.getPhone().equals("")){
                        list.add(cb.equal(phone, msgConsultingComplaintsDto.getPhone()));
                    }
                    if(msgConsultingComplaintsDto.getProblemType()!=null && !msgConsultingComplaintsDto.getProblemType().equals("")){
                        list.add(cb.equal(problemType, msgConsultingComplaintsDto.getProblemType()));
                    }
                }
                if(types!=null || types != ""){
                    list.add(cb.equal(msgType, types));

                }
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                cq.orderBy(cb.desc(root.get("createTime")));
                return null;
            }
        }, pageable);

        return res;
    }

    @Override
    public void saveMsgConsultingRecord(Long msgComplaintId, MsgConsultingRecordDto msgConsultingRecordDto,String describePicture) {
        User u = securityUtil.getCurrUser();
        MsgConsultingComplaints mcc=get(msgComplaintId);

        if(u.getUserType() == CommonConstant.USER_TYPE_ADMIN && mcc.getHandleState()==1){
            mcc.setHandleState(2);
            save(mcc);
        }
        if(mcc.getHandleState()==3){
            throw new FreightException("处理已完结");
        }
        MsgConsultingComplaintsRecord mccr=new MsgConsultingComplaintsRecord();
        ObjectUtil.copyPropertiesIgnoreNull(msgConsultingRecordDto, mccr);
        mccr.setMsgConsultingComplaintsId(msgComplaintId);
        if (describePicture!=null){
            mccr.setDescribePicture(describePicture);
        }
        if (u.getUserType() == CommonConstant.USER_TYPE_ADMIN){
            mccr.setType(2);
        }else {
            mccr.setType(1);
        }
        msgConsultingComplaintsRecordDao.save(mccr);
    }

    @Override
    public void saveMsgConsulting(Long orderId,MsgConsultingDto msgConsultingDto,String type,String describePicture) {
        User u = securityUtil.getCurrUser();
        OrderOrder orderOrder=new OrderOrder();
        MsgConsultingComplaints msgConsultingComplaints=new MsgConsultingComplaints();
        ObjectUtil.copyPropertiesIgnoreNull(msgConsultingDto, msgConsultingComplaints);
        if(orderId!=null&&orderId>0){

            orderOrder=orderOrderService.get(orderId);
            msgConsultingComplaints.setOrderNo(orderOrder.getOrderNo());
            orderOrderService.save(orderOrder);


        }
        msgConsultingComplaints.setUserId(u.getId());
        msgConsultingComplaints.setOrderId(orderId);
        msgConsultingComplaints.setDescribePicture(describePicture);
        msgConsultingComplaints.setHandleState(1);
        msgConsultingComplaints.setMsgType(type);
        if (u.getUserType() == CommonConstant.USER_TYPE_ADMIN){
            msgConsultingComplaints.setType(3);
        }else if  (u.getUserType() == CommonConstant.USER_TYPE_DRIVER){
            msgConsultingComplaints.setType(1);
        }else if  (u.getUserType() == CommonConstant.USER_TYPE_COMPANY){
            msgConsultingComplaints.setType(2);
        }else {

        }
        msgConsultingComplaintsDao.save(msgConsultingComplaints);
        MsgConsultingComplaintsRecord mccr=new MsgConsultingComplaintsRecord();
        mccr.setMsgConsultingComplaintsId(msgConsultingComplaints.getId());
        mccr.setDescribes(msgConsultingComplaints.getDescribes());
        mccr.setDescribePicture(msgConsultingComplaints.getDescribePicture());
        mccr.setDescribePicture(describePicture);
        if (u.getUserType() == CommonConstant.USER_TYPE_ADMIN){
            mccr.setType(2);
        }else {
            mccr.setType(1);
        }
        msgConsultingComplaintsRecordDao.save(mccr);
    }

    @Override
    public void endMsgConsulting(Long msgConsultingId) {
        MsgConsultingComplaints mcc=msgConsultingComplaintsDao.getOne(msgConsultingId);
        if (mcc!=null){
            mcc.setHandleState(3);
            msgConsultingComplaintsDao.save(mcc);

        }
    }
}
